package com.eventosgasteiz.utils.maps;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Address;

import com.eventosgasteiz.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MapOverlay extends Overlay {

	public static final String CIRCLE = "circulo";
	public static final String PUSHPIN = "chincheta";
	
	private static final int CONTAINER_RADIUS				= 4;
	private static final int CONTAINER_SHADOW_OFFSET		= 1;
	
	private Address address;
	private String tipo;
	private GeoPoint geopoint;
	
	public MapOverlay(Address address, String tipo) {
		super();
		assert(null != address);
		this.setAddress(address);
		this.setTipo(tipo);
		Double convertedLongitude = address.getLongitude() * 1E6;
		Double convertedLatitude = address.getLatitude() * 1E6;

		setGeopoint(new GeoPoint(
				convertedLatitude.intValue(),
				convertedLongitude.intValue()));
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
    	Point locationPoint = new Point();
    	Projection projection = mapView.getProjection();
    	projection.toPixels(getGeopoint(), locationPoint);
    	Paint containerPaint = new Paint();
		containerPaint.setAntiAlias(true);
    	int containerX = locationPoint.x;
    	int containerY = locationPoint.y;
    	if (PUSHPIN.equals(this.getTipo())){
    		drawPushpin(canvas, mapView, containerX, containerY);
    	} else if (CIRCLE.equals(this.getTipo())){
			if (shadow) {
				containerX += CONTAINER_SHADOW_OFFSET;
				containerY += CONTAINER_SHADOW_OFFSET;
				containerPaint.setARGB(90, 0, 0, 0);
	            canvas.drawCircle(containerX, containerY, CONTAINER_RADIUS, containerPaint);
			} else {
				containerPaint.setColor(Color.RED);
	            canvas.drawCircle(containerX, containerY, CONTAINER_RADIUS, containerPaint);
			}
    	} else {
    		drawPushpin(canvas, mapView, containerX, containerY);
    	}
	}
	
	
	public void drawPushpin(Canvas canvas, MapView mapView, int containerX, int containerY) {
    	//---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(
        		mapView.getResources(), R.drawable.pushpin);            
        canvas.drawBitmap(bmp, containerX, containerY-50, null);    
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Address getAddress() {
		return address;
	}
	
	public void setTipo(String tipo) {
		this.tipo = tipo;
	}

	public String getTipo() {
		return tipo;
	}

	public void setGeopoint(GeoPoint geopoint) {
		this.geopoint = geopoint;
	}

	public GeoPoint getGeopoint() {
		return geopoint;
	}

}
